package com.google.denta.dao.ibatis;

import java.util.*;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.google.denta.dao.UserDao;
import com.google.denta.entity.PaginatorForm;
import com.google.denta.entity.User;

/**
 * @author crash
 */
@SuppressWarnings("unchecked")
public final class UserSqlMapDao extends SqlMapClientDaoSupport implements
        UserDao {

    /** {@inheritDoc} */
    public Long create(final User newInstance) {
        return (Long) getSqlMapClientTemplate().insert("User.create",
                newInstance);
    }

    /** {@inheritDoc} */
    public User read(final Long id) {
        return (User) getSqlMapClientTemplate().queryForObject("User.read", id);
    }

    /** {@inheritDoc} */
    public void update(final User transientObject) {
        getSqlMapClientTemplate().update("User.update", transientObject);
    }

    /** {@inheritDoc} */
    public void updatePassword(User user) {

    }

    /** {@inheritDoc} */
    public void delete(final Long id) {
        getSqlMapClientTemplate().delete("User.delete", id);
    }

    /** {@inheritDoc} */
    public List<User> findAll() {
        return getSqlMapClientTemplate().queryForList("User.findAll");
    }

    /** {@inheritDoc} */
    public List<User> findByGroup(final Long gid, final Boolean flag) {
        final Map<String, Object> map = new HashMap<String, Object>();
        map.put("group_id", gid);
        if (flag != null) {
            map.put("group_users", flag);
        }
        return getSqlMapClientTemplate().queryForList("User.findByGroup", map);
    }

    /** {@inheritDoc} */
    public User findByName(final String name) {
        return (User) getSqlMapClientTemplate().queryForObject(
                "User.findByName", name);
    }

    /** {@inheritDoc} */
    public void deleteUserRoles(final Long id) {
        getSqlMapClientTemplate().delete("User.deleteUserRoles", id);
    }

    /** {@inheritDoc} */
    public void createUserRole(final Long uid, final Long rid) {
        final Map<String, Long> map = new HashMap<String, Long>();
        map.put("uid", uid);
        map.put("rid", rid);
        getSqlMapClientTemplate().insert("User.createUserRole", map);
    }

    /** {@inheritDoc} */
    public User getByPassword(final Long id, final String password) {
        final List<User> userList = getSqlMapClientTemplate().queryForList(
                "User.findByPassword", password);
        User user = null;
        for (User usr : userList) {
            if (usr.getId().equals(id)) {
                user = usr;
            }
        }
        return user;
    }

    /** {@inheritDoc} */
    public void createUserGroup(final Long uid, final Long gid) {
        final Map<String, Long> map = new HashMap<String, Long>();
        map.put("uid", uid);
        map.put("gid", gid);
        getSqlMapClientTemplate().insert("User.createUserGroup", map);
    }

    /** {@inheritDoc} */
    public void deleteUserGroups(final Long id) {
        getSqlMapClientTemplate().delete("User.deleteUserGroups", id);
    }

    /** {@inheritDoc} */
    public List<User> paginatorFind(final PaginatorForm form) {
        return getSqlMapClientTemplate().queryForList("User.findAll",
                form.getStartFrom().intValue(),
                form.getCountOnPage().intValue());

    }

    /** {@inheritDoc} */
    public Long getUsersCount() {
        return (Long) getSqlMapClientTemplate().queryForObject("User.count");
    }

    /** {@inheritDoc} */
    public User getByPersone(Long id) {
        return (User) getSqlMapClientTemplate().queryForObject(
                "User.findByPersone", id);
    }

}
